<?php
class OrderController extends \BaseController {

    public function __construct() {
        $this->beforeFilter('auth');
        $this->userId = Auth::user()->id;
    }


    //订单首页
    public function getIndex() {

        $orders = Order::whereRaw('user_id = ? and status = ?', array($this->userId, 0))->get();


        foreach($orders as $key => $order) {
            $products = DB::table('order_item')
                        ->join('pictures', 'order_item.picture_id', '=', 'pictures.id')
                        ->where('order_item.order_id', '=', $order->id)->get();

            $orders[$key]->products = $products;
        }

        return View::make('front/order_index')
                        ->with('orders', $orders);


    }

    //订单详情
    public function getInfo($orderId) {
        $order = Order::whereRaw('user_id = ? and id = ?', array($this->userId, $orderId))->first();

        if($order) {
            $products = DB::table('order_item')
                                ->join('pictures', 'order_item.picture_id', '=', 'pictures.id')
                                ->where('order_item.order_id', '=', $order->id)->get();


            return View::make('front/order_info')
                            ->with('order', $order)
                            ->with('products', $products);
        } else {
            return App::abort(404);
        }
    }

    //创建订单
    public function getCreate() {
        $cartItems = DB::table('shopping_cart')
                        ->join('pictures', 'shopping_cart.picture_id', '=', 'pictures.id')
                        ->where('shopping_cart.user_id', $this->userId)->get();

        $order = new Order();
        $order->user_id = $this->userId;
        $order->save();

        $orderItemArray = array();
        foreach($cartItems as $item) {
            $orderItem = array(
                'order_id' => $order->id,
                'picture_id' => $item->picture_id,
                'price' => $item->price
            );

            array_push($orderItemArray, $orderItem);
        }

        DB::table('order_item')->insert($orderItemArray);
        DB::table('shopping_cart')->where('user_id', '=', $this->userId)->delete();
        Session::put('cartNum', 0);
        return Redirect::to('/order/info/' . $order->id)
                        ->with('message', '提交订单成功，请支付！');
    }

    //取消订单
    public function getDelete($orderId) {

        $order = Order::whereRaw('user_id = ? and id = ?', array($this->userId, $orderId))->first();

        if($order) {
            DB::table('order_item')->where('order_id', '=', $orderId)->delete();
            Order::whereRaw('id = ? and user_id = ?', array($orderId, $this->userId))->delete();

            return Redirect::to('/order')
                                ->with('message', '您已取消了一个订单！');
        } else {
            return App::abort(404);
        }


    }

    //从订单中删除产品
    public function getDeleteProduct($orderId, $orderItemId) {

        $order = Order::whereRaw('user_id = ? and id = ?', array($this->userId, $orderId))->first();

        if($order){

            DB::table('order_item')->whereRaw('picture_id = ? and order_id = ?', array($orderItemId, $order))->delete();

            $orderItemNum = DB::table('order_item')->where('order_id', '=', $orderId)->count();

            if($orderItemNum == 0) {
                Order::where('id', '=', $orderId)->delete();

                return Redirect::to('/order')
                                ->with('message', '您已取消了一个订单！');
            }

            return Redirect::to('/order/info/'. $orderId)
                                ->with('message', '');

        }else {
            return App::abort(404);
        }
    }

}
